#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
cron: 49 5 * * *
new Env('标准网积分签到')
"""
# 这是下载规程规范的网站，标准网
# 功能：标准网签到签到，可以获取积分，积分能下载规程规范
# 网址：https://www.biaozhun.org/member/
# 变量：BIAOZHUN_COOKIE，多个账号需要建多个BIAOZHUN_COOKIE
# 其他：此版本使用cookie，网上没找到现成的脚本，先用这个吧，至少能用
# 脚本日期：2025年9月4日
import requests
import os

# ---------- 聚合推送配置 ----------
AGGRE_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dailylog')

IMMEDIATE = False          # True 实时推送，False 汇总推送
def push(title, text=""):
    if IMMEDIATE:
        from notify import send
        send(title, text)
    else:
        with open(AGGRE_FILE, "a", encoding="utf-8") as f:
            f.write(f"{title}\n{text}\n{'-'*40}\n")

# 签到请求的URL
url = "https://www.biaozhun.org/member/ajax_sign.php"

# 请求头信息,这里改用环境变量的cookie了
headers = {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Length": "0",
    "Cookie": "",
    "Host": "www.biaozhun.org",
    "Origin": "https://www.biaozhun.org",
    "Pragma": "no-cache",
    "Referer": "https://www.biaozhun.org/member/",
    "Sec-Ch-Ua": "\"Not;A=Brand\";v=\"99\", \"Microsoft Edge\";v=\"139\", \"Chromium\";v=\"139\"",
    "Sec-Ch-Ua-Mobile": "?0",
    "Sec-Ch-Ua-Platform": "\"Windows\"",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0",
    "X-Requested-With": "XMLHttpRequest"
}

if __name__ == "__main__":
    print("开始执行标准网站签到任务")
    # 这里换成从环境变量获取cookie
    cookie = os.getenv('BIAOZHUN_COOKIE', '').strip()
    if not cookie:
        raise RuntimeError('环境变量 BIAOZHUN_COOKIE 未设置')

    # 支持多cookie
    cookies = [c.strip() for c in cookie.split('\n') if c.strip()]
    for idx, ck in enumerate(cookies, 1):
        print(f'账号{idx} 开始任务...')
        headers['Cookie'] = ck
        try:
            # 发送POST请求
            response = requests.post(url, headers=headers)
            # 打印响应状态码和内容
            msg = f'https://www.biaozhun.org/member/ \n\n 签到响应内容: {response.text} \n\n 签到请求状态码: {response.status_code}'
            print(msg)
            push('标准网站签到成功', msg)
                
        except Exception as e:
            result = f"❌ 签到请求发生错误，更新cookie试试: {str(e)}"
            print(result)
            push('❌ 标准网站签到失败', result)
